import org.omg.CORBA.TIMEOUT;
import sun.nio.ch.ThreadPool;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

public class Thedapool {
    private int corepoolsize=1;
    private  int maxpoolvalue=5;
    private  long keepalivetime =3;
    private TimeUnit timeout= TimeUnit.SECONDS;
    private BlockingDeque<Runnable> workqueue;
    private RejectedExecutionHandler handler;
    private static AtomicLong aLong=new AtomicLong(0);

    public void run() throws InterruptedException{
        ThreadPoolExecutor pool=new ThreadPoolExecutor(corepoolsize,maxpoolvalue,
                keepalivetime,timeout,new ArrayBlockingQueue<Runnable>(3),
                new ThreadPoolExecutor.DiscardOldestPolicy()){
            @Override
            protected void beforeExecute(Thread t, Runnable r){
                System.out.println("before");
            }
            // 线程执行之后运行
            @Override
            protected void afterExecute(Runnable r, Throwable t) {
                System.out.println("...............afterExecute");
            }

            // 整个线程池停止之后
            @Override
            protected void terminated() {
                System.out.println("...............thread stop");
            }
        };

        for (int i = 1; i <= 20; i++) {
            pool.execute(new ThreadPoolTask(i, aLong));
        }

        pool.shutdown();
        Thread.sleep(2500);
        System.out.println(aLong.get());
    }


    public static void main(String[] args) {
        try {
            new Thedapool().run();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class ThreadPoolTask implements Runnable{
    private  int i =0;
    private AtomicLong along;

    public ThreadPoolTask(int i, AtomicLong along) {
        this.i = i;
        this.along = along;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(500);
            along.addAndGet(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+i);

    }
}